package com.ruoyi.common.utils;

import java.lang.management.ManagementFactory;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 时间工具类
 *
 * @author ruoyi
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        String days=day==0L?"":day+"天";
        String hours=hour==0L?"":hour+"小时";
        String mins=min==0L?"":min+"分钟";
        return days+hours+mins;
    }

    public static String convert(String srcDateStr, String srcFmt, String destFmt) {
        DateTime parse = DateUtil.parse(srcDateStr, srcFmt);
        String format = DateUtil.format(parse, destFmt);
        return format;
    }

    /**
     * 一天的开始时间
     * @return
     */
    public static Date getDayStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 一天的结束时间
     * @return
     */
    public static Date getDaySEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }
    public static String  countOpenTableTime(long startTimeMinute, BigDecimal stopTime, Boolean minuteFlag, Boolean hours, Boolean day) {
        Long newTime = new Date().getTime();
        long miao = (newTime - startTimeMinute) / 1000;//秒
//        计算时间
        BigDecimal minute = countTime(miao, true, false, false);
        if (stopTime != null) {//计算暂停时间
            minute = minute.subtract(stopTime);
        }

        BigDecimal hour = countTime(miao, false, true, false);
        if (stopTime != null) {//计算暂停时间
            hour = (hour.multiply(new BigDecimal(60))).subtract(stopTime);
        }
        BigDecimal days = countTime(miao, false, false, true);

        if (minuteFlag) {
            return minute.toString();
        }
        if (hours) {
            return hour.toString();
        }
        if (day) {
            return day.toString();
        }

        String useTime = "";//时间字符串
        if (!String.valueOf(hour).equals("0.0") || Double.valueOf(hour.toString()) >= 1.0) {
            useTime = hour + "小时";
        }
        if (!String.valueOf(minute).equals("0.0") || Double.valueOf(minute.toString()) >= 1.0) {
            useTime = useTime + minute + "分钟";
        }
        Long useHours = miao / 3600;
        Long useMinutes = (miao % 3600) / 60;
        useTime = useHours + "小时" + useMinutes + "分钟";
        return useTime;
    }


    /**
     * 计算分钟和小时和天 精确到秒数向上取整 例如 70秒=1.2分钟
     *
     * @param miao
     * @return
     */
    private  static  BigDecimal countTime(long miao, Boolean minute, Boolean hour, Boolean day) {

        if (minute) {//秒转分
            BigDecimal minutes = new BigDecimal(miao).divide(new BigDecimal(60), 2, BigDecimal.ROUND_UP).setScale(1, BigDecimal.ROUND_HALF_UP);
            return minutes;
        }
        if (hour) {//分转时
            BigDecimal minutses = new BigDecimal(miao).divide(new BigDecimal(60), 2, BigDecimal.ROUND_UP).setScale(1, BigDecimal.ROUND_HALF_UP);
            return minutses.divide(new BigDecimal(60), 2, BigDecimal.ROUND_UP).setScale(1, BigDecimal.ROUND_HALF_UP);
        }
        //时转天
        BigDecimal minutses = new BigDecimal(miao).divide(new BigDecimal(60), 2, BigDecimal.ROUND_UP).setScale(1, BigDecimal.ROUND_HALF_UP);
        BigDecimal hours = minutses.divide(new BigDecimal(60), 2, BigDecimal.ROUND_UP).setScale(1, BigDecimal.ROUND_HALF_UP);
        return hours.divide(new BigDecimal(24), 2, BigDecimal.ROUND_UP).setScale(1, BigDecimal.ROUND_HALF_UP);
    }
}
